<?php

/**
 * invoice actions.
 *
 * @package    Serag
 * @subpackage invoice
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class invoiceActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $this->forward('default', 'module');
  }
  
  
  
    public function executeAddInvoice(sfWebRequest $request) {
        $this->count = $request->getParameter('count', 1);
        $this->form = new InvoiceForm($this->count);
        if ($request->isMethod(sfRequest::POST)) {

            $ValuesArray = $request->getParameter($this->form->getName());
//            print_r($ValuesArray);exit;
            $this->form->bind($ValuesArray, $request->getFiles($this->form->getName()));

            if ($this->form->isValid()) {
                $amount = 0;
                $invoice = new Invoice();
                $invoice->setInvoiceCode($ValuesArray['invoice_code']);
                $invoice->setCustomerId($ValuesArray['customer_id']);
                $invoice->setLineId($ValuesArray['line_id']);
                $invoice->setUserId($this->getUser()->getAttribute('User')->getUserCodeNum());
                $invoice->setCreatedAt(date("Y-m-d H:i:s"));
                $invoice->setDate($ValuesArray['date']);
                for($i=1; $i<$this->count+1; $i++){
                    $invoiceProduct = new InvoiceProduct();
                    $invoiceProduct->setProductId($ValuesArray['product_id_'.$i]);
                    $invoiceProduct->setUnitId($ValuesArray['unit_id_'.$i]);
                    $invoiceProduct->setQty($ValuesArray['qty_'.$i]);
                    $invoiceProduct->setPrice($ValuesArray['price_'.$i]);
                    $amount += ($ValuesArray['price_'.$i] * $ValuesArray['qty_'.$i]);
                    $invoice->getInvoiceProduct()->add($invoiceProduct);
                }
                 
                if($ValuesArray['discount_type'] == 'percentage' && $ValuesArray['discount']) // percentage
                     $amount = $amount - (($ValuesArray['discount']/100)*$amount);
                 if($ValuesArray['discount_type'] == 'cash' && $ValuesArray['discount']) // cash
                     $amount = $amount - $ValuesArray['discount'];
    
                $invoice->setAmount($amount);
                $invoice->setDiscount($ValuesArray['discount']);
                $invoice->setDiscountType(($ValuesArray['discount'])?$ValuesArray['discount_type']:null);
                $invoice->save();
                return $this->renderText('Done');
            }
        }
    }
  
  
    public function executeEditInvoice(sfWebRequest $request) {
//        echo "<pre>";
//        print_r($x = InvoiceTable::getInvoiceDetails(111111));
//        print_r(InvoiceTable::formatEditInvoice($x));
//        echo "</pre>";exit;
        $this->invoiceCode = $request->getParameter('id');
        $invoiceData = InvoiceTable::formatEditInvoice(InvoiceTable::getInvoiceDetails($this->invoiceCode));
        $this->count = $request->getParameter('count', $invoiceData['count']);
        $this->form = new InvoiceForm($this->count, $this->invoiceCode);
        $this->form->setDefaults($invoiceData);
        if ($request->isMethod(sfRequest::POST)) {

            $ValuesArray = $request->getParameter($this->form->getName());
            ;
            $this->form->bind($ValuesArray);

            if ($this->form->isValid()) {
                Doctrine_Query::create()->delete()->from('InvoiceProduct')->where('invoice_code = ?', $this->invoiceCode)->execute();
                $amount = 0;
                $invoice = Doctrine::getTable('Invoice')->findOneByInvoiceCode($this->invoiceCode);
                $invoice->setInvoiceCode($ValuesArray['invoice_code']);
                $invoice->setCustomerId($ValuesArray['customer_id']);
                $invoice->setLineId($ValuesArray['line_id']);
                $invoice->setUserId($this->getUser()->getAttribute('User')->getUserCodeNum());
                $invoice->setDate($ValuesArray['date']);
                for($i=1; $i<$this->count+1; $i++){
                    $invoiceProduct = new InvoiceProduct();
                    $invoiceProduct->setProductId($ValuesArray['product_id_'.$i]);
                    $invoiceProduct->setUnitId($ValuesArray['unit_id_'.$i]);
                    $invoiceProduct->setQty($ValuesArray['qty_'.$i]);
                    $invoiceProduct->setPrice($ValuesArray['price_'.$i]);
                    $amount += ($ValuesArray['price_'.$i] * $ValuesArray['qty_'.$i]);
                    $invoice->getInvoiceProduct()->add($invoiceProduct);
                }
                 
                if($ValuesArray['discount_type'] == 'percentage' && $ValuesArray['discount']) // percentage
                     $amount = $amount - (($ValuesArray['discount']/100)*$amount);
                 if($ValuesArray['discount_type'] == 'cash' && $ValuesArray['discount']) // cash
                     $amount = $amount - $ValuesArray['discount'];
    
                $invoice->setAmount($amount);
                $invoice->setDiscount($ValuesArray['discount']);
                $invoice->setDiscountType(($ValuesArray['discount'])?$ValuesArray['discount_type']:null);
                $invoice->save();
                return $this->renderText('Done');
            }
        }
    }
    
    
}
